
#include <Stepper.h>
#include <Servo.h>
#define STEPS 512 //一圈为512转
// 定义引脚的连接
Servo oneservo;//定义舵机
int inputPin = A1;//电位器模拟信号输入
int outputPin = 2;//特意增加一个电源输出
int pos = 0;//记录当前舵机位置
int lastpos = 0;//记录上次舵机位置
Stepper stepper(STEPS, 8, 10, 9, 11); //初始化电机接口
void setup() 
{ 
 Serial.begin(9600);
 oneservo.attach(6);//6号为舵机信号输入
 pinMode(inputPin,INPUT);
 pinMode(outputPin,OUTPUT);
} 
//主循环
void loop() 
{ 
  digitalWrite(outputPin,HIGH);
  int reading = analogRead(inputPin);//电位器模拟信号输入
  int dal = map(reading,0,1023,0,180);//舵机数据适当放缩
  int val = map(reading,0,1023,0,50);//步机数据适当放缩
  Serial.println(val);
  pos = dal;//初始化
  //以下程序思路如下若模拟信号增大现实仪表往原方向偏转更多
  //信号减少仪表盘往反方向偏转
  //信号不变则不偏转
  //下面个循环分别对应如上三种情况
  if(lastpos < pos)
  {
  for(pos = lastpos; pos < dal; pos++)
  {
    oneservo.write(pos);
    delay(15);
  }
    lastpos = pos;
  }
  else if(lastpos > pos)
  {
    for(pos = lastpos; pos > dal; pos--)
    {
       oneservo.write(pos); 
       delay(15);
    }
       lastpos = pos;
  }
  else
  {
      lastpos = pos; 
      delay(15);
  }
 //改变步机速度
 stepper.step(val); 
 delay(15);
 
}
